<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{

    /**
     * 显示后台登录页
     *
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 登录验证
     * @param Request $request
     *
     * @return \think\response\Json
     */
    public function doLogin(Request $request)
    {
        $data=$request->post();
        //验证码
        if (!captcha_check($data['verify'])) {
            $re = ['msg'=>'验证码非法','code'=>'2'];
            return json($re);
        }
        //表单验证
        $result = $this->validate($data,'app\admin\validate\Login');
        if(true !== $result)
        {
            return json(['msg'=>$result,'code'=>'2']);
        }
        //数据对比
        if($data['uname'] === config('user.uname') && $data['pwd'] === config('user.pwd'))
        {
            //登录成功
            session('sid',$data['uname']);
            return json(['code'=>'1']);
        }

    }

    /**
     * 退出登录
     */
    public function out()
    {
        session("sid",null);
        return $this->redirect("/");
    }
}
